<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Contactus extends My_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index() {
        $posts = $this->input->post();
        $data['post'] = $posts;
        $data['public_key'] = $this->config->item('public_key');
        $data['error'] = null;
        $this->load->helper('email');
        if ($posts) {
            $privateKey = $this->config->item('private_key');
            $validationErrors = array();

            if ($posts['message'] == '') {
                $validationErrors['message'] = "Message cannot be blank";
            }
            if ($posts['name'] == '') {
                $validationErrors['name'] = "Your Name cannot be blank";
            }

            if ($posts['email'] == '') {
                $validationErrors['emailAddress'] = "Email cannot be blank";
            } elseif (!valid_email($posts['email'])) {
                $validationErrors['emailAddress'] = "Email is not valid";
            }

            if ((!isset($posts['recaptcha_challenge_field']) || !isset($posts['recaptcha_challenge_field'])) && !isset($posts['captcha_code'])) {
                $validationErrors['recaptcha_response_field'] = 'Please enter captcha';
            }

            if (isset($posts['recaptcha_challenge_field']) && isset($posts['recaptcha_challenge_field'])) {
                $resp = recaptcha_check_answer($privateKey, $_SERVER["REMOTE_ADDR"], $posts["recaptcha_challenge_field"], $posts["recaptcha_response_field"]);
                if (!$resp->is_valid) {
                    $validationErrors['recaptcha_response_field'] = $resp->error;
                }
            }
            
            if(isset($posts['captcha_code'])){
                $currentCaptcha = $this->session->userdata('captcha_mobile');
                if(strtolower($currentCaptcha) != strtolower($posts['captcha_code'])){
                    $validationErrors['captcha_code'] = 'Incorrect Captcha';
                }
            }
            

            if (count($validationErrors) != 0) {
                $data['usermessage'] = array('error', 'Please complete all required fields');
                $data['fielderrors'] = $validationErrors;
            } else {
                $content = '<b>Your Name:</b> ' . $posts['name'] . '<br>';
                $content .= '<b>Email Address</b>: ' . $posts['email'] . '<br>';
                $content .= '<b>Mobile</b>: ' . $posts['mobile'] . '<br>';
                $content .= '<b>Content</b>: ' . strip_tags($posts['message']);
                if (sendmail(null, 'Contact Us', $content, $posts['email'], $posts['name'])) {
                    $data['usermessage'] = array('success', 'Thank you - Your contact request is being sent and a Sales Agent will be in contact with you directly');
                    $data['post'] = '';
                } else {
                    $data['usermessage'] = array('error', 'Email sent Error');
                }
            }
        }
        $this->navigation->loadContactUsView($data);
    }

}

/* End of file contactus.php */
/* Location: ./application/controllers/contactus.php */